package com.vogue.supplymall.match.domain.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.vogue.ssm.database.dao.BaseDao;
import com.vogue.supplymall.account.domain.service.BaseCodeService;
import com.vogue.supplymall.match.domain.dao.PrepayDao;
import com.vogue.supplymall.match.domain.service.PrepayService;
import com.vogue.supplymall.match.util.DealDataMethod;

@Service("prepayService")
@Transactional
public class PrepayServiceImpl extends BaseServiceImpl<Object, Long> implements PrepayService{

	@Resource
    private PrepayDao prepayDao;
	@Resource
	private BaseCodeService baseCodeService;
	
	public PrepayServiceImpl(BaseDao<Object, Long> baseDao) {
		super(baseDao);
	
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getShopOfCustomer(String syscodes) throws Exception{
		String syscodeIds="";
		Map<String, Object> sysMap=new HashMap<>();
		List<Map<String, Object>> resultList=new ArrayList<>();
		List<Map<String, Object>> finalList=new ArrayList<>();
		
		String[] sysArr=syscodes.split(",");
		for(String syscode:sysArr) {
			Long code=Long.valueOf(syscode);
			Long sysCodeId =baseCodeService.checkSysCodeValidity(code);
			syscodeIds+=sysCodeId+",";
			sysMap.put(sysCodeId+"", syscode);
		}
		
		syscodeIds=syscodeIds.substring(0,syscodeIds.length()-1);
		List<Map<String, Object>> list=prepayDao.getShopOfCustomer(syscodeIds);
		
		//处理结果
		if(list.size()>0&&list.get(0)!=null) {
			Map<String, Object>  paramMap=new HashMap<>();
			List<String> speList=new ArrayList<>();
			
			speList.add("detail_");
			paramMap.put("diffType", "syscodeid");
			paramMap.put("speCol", speList);
			
			resultList=DealDataMethod.dealDataMethod(list, paramMap);
			
			//将syscodeid的值替换为syscode
			if(resultList.size()>0&&resultList.get(0)!=null) {
				for(Map<String, Object> rMap:resultList) {
					Map<String, Object> finalMap=new HashMap<>();
					
					String sysId=rMap.get("syscodeid").toString();
					List<Map<String, Object>> tempList=(List<Map<String, Object>>) rMap.get("detail");
					String tempCode=(String) sysMap.get(sysId);
					
					finalMap.put("syscode", tempCode);
					finalMap.put("detail", tempList);
					
					finalList.add(finalMap);
					
				}
			}
		}
		
		return finalList;
	}

}
